package router

import (
	platformController "gitee.com/wangshh_123/cross-pay-go/src/controller/platform"
	systemController "gitee.com/wangshh_123/cross-pay-go/src/controller/system"
	"gitee.com/wangshh_123/cross-pay-go/src/database"
	"gitee.com/wangshh_123/cross-pay-go/src/middleware"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/integration"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/hlPay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/juswaypay"
	"gitee.com/wangshh_123/cross-pay-go/src/platform/providers/photonpay"
	"gitee.com/wangshh_123/cross-pay-go/src/service"
	"gitee.com/wangshh_123/cross-pay-go/src/utils/websocket"

	"github.com/gin-gonic/gin"
)

// SetupRouter 设置路由
func SetupRouter(r *gin.Engine, photonPayClient *photonpay.PhotonPayClient, juswayPayClient *juswaypay.JuswayPayClient, hlPayClient *hlPay.HlPayClient) {
	// 使用自定义的彩色日志中间件
	r.Use(middleware.Logger())
	r.Use(gin.Recovery())

	// 使用内存监控中间件
	r.Use(middleware.MemoryMonitor())

	// 全局中间件
	r.Use(middleware.Cors())

	// 设置 API 路由组
	api := r.Group("/api")
	{
		v1 := api.Group("/v1")
		{
			// 用户相关路由
			SetupUserRouter(v1, database.DB, photonPayClient, hlPayClient)

			// 账号管理路由
			SetupAccountRouter(v1, photonPayClient)

			// 设置 PhotonPay 路由
			SetupPhotonPayRouter(v1, photonPayClient, hlPayClient)

			// 注册 webhook 路由
			webhookController := platformController.NewWebhookController(photonPayClient)
			RegisterWebhookRoutes(v1, webhookController)

			// 聚实汇相关路由
			SetupJuswayPayRouter(v1, juswayPayClient)

			// HlPay相关路由
			SetupHlPayRouter(v1, hlPayClient)

			// 统一的卡BIN路由
			SetupCardBinRouter(v1)

			// 统一的卡片CVV路由
			SetupCardCVVRouter(v1)

			// 统一的卡片详情路由
			SetupCardDetailRouter(v1)

			// 统一的持卡人路由
			SetupCardholderRouter(v1)

			// 统一的卡历史路由
			SetupCardHistoryRouter(v1)

			// HlPay账户余额路由
			SetupHlPayAccountBalanceRouter(v1)

			// 设置统一卡列表路由
			SetupCardListRouter(v1, photonPayClient, hlPayClient)

			// 管理员相关路由
			SetupAdminRouter(v1, database.DB, photonPayClient, hlPayClient)

			// 注册部门管理路由
			SetupDepartmentRouter(v1)

			// 数据相关路由
			SetupStatisticsRouter(v1, photonPayClient)

			// 收款明细路由
			SetupReceiptRouter(v1)

			// WebSocket 消息推送路由
			v1.GET("/ws", websocket.ServeWs)

			// 注册通知相关路由
			RegisterNotificationRoutes(v1)

			// 余额同步路由
			SetupBalanceSyncRoutes(r)

			// 卡片余额同步路由
			SetupCardBalanceSyncRoutes(r, database.DB)

			// 统一交易路由
			platformManager := integration.NewPlatformManagerWithClients(photonPayClient, hlPayClient)
			unifiedTransactionController := platformController.NewUnifiedTransactionController(platformManager)
			SetupTransactionRoutes(v1, unifiedTransactionController)

			// HlPay交易缓存路由 - 使用统一的/api/v1格式
			hlpayCacheService := service.NewHlPayTransactionCacheService(hlPayClient, nil)
			hlpayCacheController := systemController.NewHlPayTransactionCacheController(hlpayCacheService)
			SetupHlPayTransactionCacheRoutes(v1, hlpayCacheController)

			// 用户信息路由
			cardListController := platformController.NewCardListController(photonPayClient, hlPayClient, database.DB)
			userInfoController := systemController.NewUserInfoController(cardListController)
			SetupUserInfoRoutes(v1, userInfoController)

			// 时区相关路由
			SetupTimezoneRoutes(v1)
		}
	}
}
